#!/usr/bin/env python3.9
# -*- encoding: utf-8 -*-
"""
@文件        :models.py
@说明        :
@时间        :2022/11/30 13:49:40
@作者        :Mars
@版本        :1.0
"""


import os
import time

from django.conf import settings
from django.db import models

from mars.utils.models import BaseModel

# Create your models here.


def cert_path(instance, filename):
    return "/".join([settings.CERT_ROOT, filename])


# 小程序参数模型
class WechatConfig(BaseModel):
    app_id = models.CharField(max_length=36, unique=True, verbose_name="AppID")
    app_secret = models.CharField(max_length=64, verbose_name="AppSecret")
    mch_id = models.CharField(max_length=20, null=True, blank=True, verbose_name="商户号")
    mch_serialno = models.CharField(
        max_length=128, null=True, blank=True, verbose_name="商户证书序列号"
    )
    apiv3_key = models.CharField(
        max_length=64, null=True, blank=True, verbose_name="APIV3密钥"
    )
    mch_private_key = models.FilePathField(
        path=settings.CERT_ROOT,
        max_length=300,
        verbose_name="商户API证书",
        blank=True,
        null=True,
    )
    upload_key = models.FilePathField(
        path=settings.CERT_ROOT,
        max_length=300,
        verbose_name="上传密钥",
        blank=True,
        null=True,
    )
    domain = models.CharField(
        max_length=30, null=True, blank=True, verbose_name="支付回调域名"
    )

    def __str__(self) -> str:
        return self.app_id

    class Meta:
        db_table = "tb_weconfig"
        verbose_name = "小程序后端配置"
        verbose_name_plural = verbose_name


# 阿里云参数模型
class AliConfig(BaseModel):
    # AccessKey 访问密钥
    accesskey_id = models.CharField(
        max_length=64, verbose_name="用户标识", help_text="AccessKey ID"
    )
    accesskey_secret = models.CharField(
        max_length=64, verbose_name="用户密钥", help_text="AccessKey Secret"
    )

    # 对象存储OSS
    endpoint = models.CharField(
        max_length=40,
        verbose_name="对外服务访问域名",
        help_text="Endpoint",
        default="oss-cn-shanghai.aliyuncs.com",
    )
    bucket_name = models.CharField(
        max_length=20, verbose_name="存储空间名称", help_text="Bucket Name"
    )
    expire_time = models.IntegerField(default=3600, verbose_name="超时时间")
    # callback_url = models.CharField(max_length=200, null=True, blank=True,default="", verbose_name="OSS_CALLBACK_URL")
    region = models.CharField(
        max_length=30,
        verbose_name="数据中心地域",
        help_text="Region ID",
        default="cn-shanghai",
    )

    class Meta:
        db_table = "tb_aliconfig"
        verbose_name = "阿里云配置"
        verbose_name_plural = verbose_name


class FuncPrice(BaseModel):
    """功能定价"""

    make_idphoto = models.DecimalField(
        max_digits=5, decimal_places=2, verbose_name="证件照价格"
    )
    change_bg = models.DecimalField(
        max_digits=5, decimal_places=2, verbose_name="更改背景价格"
    )
    clip_photo = models.DecimalField(
        max_digits=5, decimal_places=2, verbose_name="裁剪照片价格"
    )
    compress_photo = models.DecimalField(
        max_digits=5, decimal_places=2, verbose_name="压缩照片价格"
    )
    human_style = models.DecimalField(
        max_digits=5, decimal_places=2, verbose_name="风格化照片价格"
    )

    class Meta:
        db_table = "tb_func_price"
        verbose_name = "功能定价"
        verbose_name_plural = verbose_name


def modify_path(instance, filename):
    """
    重定义图片保存路径
    :param instance: self
    :param filename: 文件名
    :return: 新路径
    """
    ext = filename.split(".").pop()
    now_time = int(time.time())
    filename = "{0}.{1}".format(now_time, ext)
    return os.path.join("app_img", filename)  # 系统路径分隔符差异，增强代码重用性


class MiniAppConfig(BaseModel):
    """系统参数设置"""

    # 小程序名称
    app_name = models.CharField(
        verbose_name="小程序名称",
        max_length=30,
    )
    # 分享标题
    share_title = models.CharField(
        verbose_name="分享标题",
        max_length=50,
        blank=True,
        null=True,
    )
    # 分享图片
    share_img = models.ImageField(
        verbose_name="分享图片",
        upload_to=modify_path,
        blank=True,
        null=True,
    )
    # 分享路径
    share_path = models.CharField(
        verbose_name="分享页面",
        null=True,
        blank=True,
        max_length=100,
        default="pages/index/index",
    )
    # 等待处理logo
    wait_logo = models.ImageField(
        verbose_name="等待处理图标",
        upload_to=modify_path,
        blank=True,
        null=True,
    )
    # 正在处理文字
    wait_text = models.CharField(
        max_length=30,
        verbose_name="等待处理显示文字",
        blank=True,
        null=True,
        default="正在处理，请稍后...",
    )

    class Meta:
        db_table = "tb_miniapp_config"
        verbose_name = "小程序客户端配置"
        verbose_name_plural = verbose_name


# class UploadCode(models.Model):
#     """小程序代码上传"""

#     upload_key = models.FilePathField(
#         path=settings.CERT_ROOT,
#         max_length=300,
#         verbose_name="上传密钥",
#         blank=True,
#         null=True,
#     )

#     class Meta:
#         db_table = "tb_upload_code"
#         verbose_name = "上传小程序代码"
#         verbose_name_plural = verbose_name
